Skip to content

bootutil: add ECIES-P384 image encryption - #2973

Draft
JayToltTech wants to merge 9 commits into
mcu-tools:mainfrom
Tolt-Technologies:feat/ecies-p384
Draft

JayToltTech wants to merge 9 commits into
mcu-tools:mainfrom
Tolt-Technologies:feat/ecies-p384

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #2972 and, through it, #2969. The first three commits here are theirs.

Signing with P-384 today leaves you encrypting with ECIES-P256 or X25519 and a SHA-256 KDF, which is weaker than the signature. CNSA 1.0 and FIPS 186-5 pair P-384 with SHA-384 and want the key establishment on the same curve. imgtool already insists on a P-384 encryption key when you sign with P-384 (main.py), but ecies_hkdf() has no P-384 branch, so it falls into the X25519 path and dies. #2889 goes the other way and lets a P-256 encryption key ride along with a P-384 signature. This is the fix I'd rather have.

The scheme is ECIES-P256 with the curve and the hash swapped. 97-byte uncompressed ephemeral point, 48-byte HMAC-SHA384 tag, then the AES-CTR wrapped key, so 161 or 177 bytes depending on AES key size. HKDF is SHA-384 with no salt and the same MCUBoot_ECIES_v1 info. SHA-384 isn't optional here, so the TLV identifies the whole scheme the way 0x32 does for P-256 and there's no SHA-256 variant to allocate later.

On the TLV value. 0x35 is the next one after ENC_X25519_SHA512 and I couldn't find an open PR or issue using it. We've had this in product from our fork since August, so there are units in the field reading 0x35 as ECIES-P384. I'd ask that it keep that value.

Only PSA implements it. encrypted_psa.c already did the ECDH through psa_key_derivation_key_agreement(), which doesn't care about the curve, so the change there is the curve family, scalar length and PKCS#8 curve OID, and P-256 and P-384 share parse_priv_enckey(). On Zephyr, CONFIG_BOOT_ENCRYPT_IMAGE with the P-384 signature type selects it, same as P-256 maps to ECIES-P256 and Ed25519 to X25519. It also turns on MBEDTLS_ECP_NIST_OPTIM, as BOOT_ECDSA_MBEDTLS does for P-256. Zephyr only offers that once an ECC key pair type is wanted, which ECIES brings in, so #2972 can't do the same for a verify-only build. It costs 1.2 KB and takes the P-384 verify on an nRF5340 DK from about 14.5 s to 1.7 s.

Commits:

  • bootutil: the TLV, sizes, PSA decrypt, allow-list entry and a caps bit
  • imgtool: encryption with a P-384 key, --hmac-sha 384, test keys, tests
  • boot: zephyr: Kconfig/CMake wiring, plus MBEDTLS_ECP_NIST_OPTIM with the built-in Mbed TLS
  • sim: enc-ec384 and enc-aes256-ec384 (need sig-ecdsa-psa sig-p384 mbedtls-v4) plus three CI rows, one job each
  • docs

Tested:

  • sim: sig-ecdsa-psa sig-p384 enc-ec384 mbedtls-v4 passes all 31 with --test-threads=1, which is how ci/sim_run.sh runs PSA rows. Run in parallel, it fails a few tests at random, and so does main with sig-ecdsa-psa sig-p384 mbedtls-v4 or sig-ecdsa-psa enc-ec256 mbedtls-v4. Switching the sim's TLV generator to HKDF-SHA256 makes 12 of them fail on "MAC verification failed", so the decrypt path really is being exercised. The AES-256 and swap-offset/validate-primary-slot/max-align-16 variants pass too, as do the existing rows I could think of that this touches: sig-ecdsa-psa with sig-p384 and with enc-ec256/enc-aes256-ec256 (3.6 and mbedtls-v4), sig-ecdsa enc-ec256, sig-ecdsa-mbedtls enc-ec256-mbedtls, enc-ec256, enc-x25519, enc-aes256-x25519, sig-ed25519 enc-x25519, enc-rsa, enc-kw and custom-enc-crypto.
  • imgtool: tests/test_encryption.py decrypts the image with the cryptography package independently of imgtool, for AES-128 and AES-256, and checks the --hmac-sha combinations are refused. The rest of the suite passes except test_keygen_type[ecdsa-p256] and [ecdsa-p384], which fail the same way on main with OpenSSL 4.1 here.
  • Zephyr: a twister case for P-384 with encryption on nrf52840dk with the same 'big' overlay, 68.7 KB of 72 KB against Zephyr v4.4.0-63. It doesn't fit the stock 64 KB partition on nrf5340dk. Also built for our own nRF5340 board with AES-256 and CONFIG_MCUBOOT_USE_TLV_ALLOW_LIST=y, 124 KB of 132 KB.
  • On device: an nRF5340 DK with a 128 KB boot partition and CDC ACM recovery, built with only CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384 and CONFIG_BOOT_ENCRYPT_IMAGE on top of the defaults (126.1 KB). I uploaded an ECIES-P384 image through serial recovery. It was AES-128 with a 22372-byte body, so not a multiple of 16. The image decrypted in place and booted, and slot 0 matched an independent decrypt byte for byte. Boot, including the verify, finishes at 1.97 s. The decrypt runs after recovery has acknowledged the last chunk and takes about 4 s (10 to 18 s without NIST_OPTIM). A host that resets the board straight after the upload leaves ciphertext in slot 0, and the board goes back into recovery. That's how recovery already works, not something this PR changes.
  • Against our fork: imgtool from this branch reproduces the fork's pinned-randomness golden images byte for byte, and both the fork's and this branch's boot_decrypt_key() recover the golden AES key from the same 0x35 TLV.

Thoughts?

The PSA ECDSA path never calls psa_crypto_init().  Ed25519, the
encryption code and the FIH delay RNG all init PSA themselves, ECDSA
just assumes someone else did.

On Zephyr the mbedtls module's SYS_INIT usually has, so it doesn't
show.  With CONFIG_MBEDTLS_INIT=n, or a port that doesn't init PSA
before boot_go(), psa_import_key() returns PSA_ERROR_BAD_STATE and
nothing ECDSA-signed validates.  Same on Mbed TLS 3.6 and 4.1.

psa_crypto_init() is a no-op when PSA is already up.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
init_crypto() called psa_crypto_init() before every boot, which
assumed the code under test would never need to do it itself.  That's
what hid the missing init in the PSA ECDSA path.  Without the previous
commit, sig-ecdsa-psa fails 12 of 31 core tests on both 3.6 and
mbedtls-v4.

The external RNG enable stays, that's harness setup.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
@JayToltTech
JayToltTech force-pushed the feat/ecies-p384 branch 3 times, most recently from d91908f to 117b32e Compare November 16, 2026 02:48
bootutil already verifies ECDSA P-384 signatures with a SHA-384 image
hash (MCUBOOT_SIGN_EC384) on the PSA Crypto backend, and the simulator
covers it with the sig-p384 feature, but the Zephyr port has no way to
select it.

Add CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384.  It selects the PSA backend,
the SHA-384 image hash and the P-384 public-key PSA wants.  The
tinycrypt, CC310 and legacy Mbed TLS ECDSA backends are P-256 only, so
no implementation choice is offered.  The default key file is
root-ec-p384.pem, which CMake already lists as a debug key, and keys.c
picks up the ecdsap384_pub_key array that imgtool getpub emits for a
P-384 key.

Also add a SHA-384 counterpart to the PSA hash dependency symbols, a
TLV size estimate for the trailer check (216 bytes: SHA-384 hash, a
SHA-384 key hash and a DER signature of up to 104 bytes), and a twister
build on nrf52840dk.  P-384 is about 5 KB larger than P-256 on PSA and
does not fit the default 48 KB MCUboot partition, so the test uses the
existing 72 KB "big" overlay.

Select MBEDTLS_ENABLE_HEAP with the built-in Mbed TLS, as BOOT_RSA_PSA
does, and default its size to 8192.  TF-PSA-Crypto allocates during
psa_crypto_init() and the P-384 verify, and boot/zephyr/prj.conf leaves
the libc malloc arena at 0, so without the heap every image is rejected.
On an nRF5340 DK, 512 and 4096 bytes both fail and 8192 boots.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
An image signed with ECDSA P-384 can only be encrypted with ECIES-P256
or ECIES-X25519 today, so the key exchange and its SHA-256 KDF sit
below the security level of the signature.  CNSA 1.0 and FIPS 186-5
pair P-384 with SHA-384, and expect the key establishment to use the
same curve.

Add ECIES-P384 as MCUBOOT_ENCRYPT_EC384 with a new TLV,
IMAGE_TLV_ENC_EC384 (0x35).  It follows ECIES-P256 exactly except for
the curve and the hash: an uncompressed 97-byte ephemeral secp384r1
point, then a 48-byte HMAC-SHA384 tag, then the AES-CTR wrapped key.
HKDF also uses SHA-384 and the same "MCUBoot_ECIES_v1" info string.
SHA-384 is fixed for this key exchange rather than a choice, so 0x35
identifies the whole scheme the way 0x32 does for ECIES-P256.

Only the PSA backend implements it.  The key agreement there is already
curve-agnostic, so encrypted_psa.c needs the curve family, scalar
length and PKCS#8 curve OID for P-384, and shares parse_priv_enckey()
with P-256.  encrypted.c keeps out of the way the same way it does for
the PSA ED25519 and ECDSA builds.  The TLV joins the unprotected
allow-list, and BOOTUTIL_CAP_ENC_EC384 lets the simulator detect it.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
imgtool already insists that a P-384 signing key is paired with a
P-384 encryption key, but ecies_hkdf() has no P-384 branch, so such an
image falls through to the X25519 path and fails.

Encrypt with ECIES-P384 when the --encrypt key is a P-384 key and emit
the ENCEC384 TLV (0x35): ECDH on secp384r1, HKDF and HMAC on SHA-384,
and the ephemeral key as an uncompressed X9.62 point. --hmac-sha
accepts 384, which is the only value ECIES-P384 takes, and auto
selects it.

Add enc-ec384-priv.pem and enc-ec384-pub.pem as test keys alongside the
other enc-* keys, and tests that decrypt the result independently with
the cryptography package and check the HMAC choices are enforced.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Enable image encryption for CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384,
mapping it to ECIES-P384 the way P-256 maps to ECIES-P256 and Ed25519
to ECIES-X25519.  CONFIG_BOOT_ENCRYPT_IMAGE selects the hidden
BOOT_ENCRYPT_EC384, which builds encrypted_psa.c and pulls in the PSA
ECDH, HKDF, HMAC and AES-CTR wants on P-384.  The default key file is
enc-ec384-priv.pem, and the trailer size check accounts for the
161/177-byte TLV when the encrypted TLV is saved.

The ECIES-P384 dependencies also select MBEDTLS_ECP_NIST_OPTIM with the
built-in Mbed TLS, as BOOT_ECDSA_MBEDTLS does for P-256.  Zephyr only
offers it once an ECC key pair type is wanted, which ECIES brings in.
On an nRF5340 DK it costs 1.2 KB and cuts the P-384 verify from about
14.5 s to 1.7 s, and the in-place serial recovery decrypt from 10-18 s
to about 4 s.

Also add a twister build of it on nrf52840dk with the "big" overlay,
68.7 KB of 72 KB.  It doesn't fit the MCUboot partition of nrf5340dk.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Add enc-ec384 and enc-aes256-ec384 features.  ECIES-P384 is implemented
only in encrypted_psa.c, so they require sig-ecdsa-psa, sig-p384 and
mbedtls-v4, and build.rs refuses any other combination.  The TLV
generator learns ENCEC384 (ECDH P-384, HKDF and HMAC on SHA-384), the
images are signed with P-384, and the embedded decryption key is the
PKCS#8 form of enc-ec384-priv.pem.  CI gains three rows, AES-128,
AES-256 and swap-offset with primary slot validation, one per job
because together they ran 5h54m against the 6h job limit.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Document the ECIES-P384 key exchange and its 0x35 TLV in
encrypted_images.md and the TLV list in design.md, and add a release
note.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
boot_decrypt_key() cleared the private key, the derived key and the
IV/key buffer with memset() just before they go out of scope, which
the compiler may drop as dead stores. Use bootutil_wipe_memory() for
these, and for the caller's enckey on failure.

Two error paths returned without clearing anything: a failed
parse_priv_enckey(), which can leave a partial private key on the
stack, and a failed psa_key_derivation_output_bytes(), which can leave
a partial derived key. Both now wipe the buffer before returning.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
@JayToltTech

Copy link
Copy Markdown
Contributor Author

Ran this on an nRF5340 DK (nrf5340dk/nrf5340/cpuapp, CDC ACM recovery), built with only the P-384 signature type and BOOT_ENCRYPT_IMAGE on top of the defaults. Images were signed and encrypted with this branch's imgtool (--sha 384 -k root-ec-p384.pem --encrypt enc-ec384-pub.pem), and the body isn't a multiple of 16 bytes.

  • Upload over recovery into slot 0: decrypted in place, byte for byte equal to an independent decrypt of the image, and it boots.
  • Plain P-384 image with the same build: boots.

The branch now also selects MBEDTLS_ECP_NIST_OPTIM if MBEDTLS_BUILTIN in the ECIES-P384 dependencies (d24d861). On the DK that takes the verify from 14.5 s to 1.7 s and the in-place decrypt from 10-18 s to about 4 s. Boot finishes at 1.97 s. The boot partition build is 126.1 KB, and the nrf52840dk twister config is 68.7 of 72 KB.

FYI for anyone testing recovery with encryption: recovery answers the last upload chunk and only then decrypts. A host that resets the device as soon as the upload returns gets a slot full of ciphertext and an "Image in the primary slot is not valid!" on the next boot. Wait for the device to answer an SMP request again before resetting. I tripped over that myself first.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant